package com.swsc.ai.tasks;

import com.swsc.ai.enums.TFEnum;
import com.swsc.ai.taskfactory.Task;
import com.swsc.ai.util.HDFSUtil;
import com.swsc.ai.util.SparkUtil;
import org.apache.spark.sql.SparkSession;

import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @describe: ndcg模板
 * @author: 容若
 * @created: 2023-12-28 9:05
 */
public abstract class NDCGTempTask implements Task {
    @Override
    public void run(Map<String, String> map) throws Exception {
        String projectName = "prodForecast";
        String taskName = "checkNDCG";
        String subTaskName = "ndcg";
        String className = map.getOrDefault("className", "NDCGTempTask");

        String ndcgNum = map.getOrDefault("ndcgNum", "10");

        String dt = map.getOrDefault("dt", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        String isTest = map.getOrDefault("isTest", TFEnum.FALSE.getName());
        String fileName = HDFSUtil.getOutHDFSPath(projectName, taskName, subTaskName, dt);
        if (TFEnum.TRUE.equals(TFEnum.fromTypeName(isTest))) {
            fileName = HDFSUtil.getTestOutHDFSPath(className, subTaskName, dt);
        }
        SparkSession session = SparkUtil.getSession(isTest, className);
        createView(session, fileName);
        calculateResult(session, dt, fileName, ndcgNum);
        if (null != session){
            session.stop();
        }
    }

    /**
     * 创建独立视图
     * @param session
     * @param str
     */
    public abstract void createView(SparkSession session, String... str) throws SQLException;

    /**
     * 计算静态特征宽表
     * @param session
     * @param dt
     * @param fileName
     */
    public abstract void calculateResult(SparkSession session, String dt, String fileName, String ndcgNum);
}
